// component/storepickup.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    title: {
      type: String,
      value: "选择自提时间"
    },
    days: {
      type: Array,
      value: [
        // {
        //   time: "今天（周一）",
        //   id: 1
        // },
        // {
        //   time: "明天（周二）",
        //   id: 2
        // },
        // {
        //   time: "后天（周三）",
        //   id: 3
        // },
        // {
        //   time: "大后天（周四）",
        //   id: 4
        // },
        // {
        //   time: "四天后（周五）",
        //   id: 5
        // },
      ]
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    modelDayId: 0,
    modelDateId: 0,
    intervalDate: ["立即自提"],
    modelHidden: true,
    animationData: {},
    dayList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    selectDay: function(options) {
      let id = options.currentTarget.dataset.id;
      const day = new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000
      console.log(id)
      if (id) {
        this.getTimeList(new Date(day))
      } else {
        this.getTimeList()
      }
      this.setData({
        modelDayId: id
      })
    },
    selectDate: function(options) {
      let id = options.currentTarget.dataset.id;
      console.log(id)
      this.setData({
        modelDateId: id
      })
      this.hideModel()
      console.log(this.properties.dayList[this.data.modelDayId], this.data.intervalDate[this.data.modelDateId])
      this.triggerEvent('pickUpTime', {
        day: this.properties.dayList[this.data.modelDayId],
        date: this.data.intervalDate[this.data.modelDateId]
      })
    },
    _getDateArray: function(endDate, splitTime, count) {
      if (!endDate) {
        endDate = new Date();
      }
      if (!splitTime) {
        splitTime = 30 * 60 * 1000;
      }
      if (!count) {
        count = 48;
      }
      let endTime = endDate.getTime();
      let start = new Date(endDate.toLocaleDateString()).getTime() + 24 * 60 * 60 * 1001;

      let mod = endTime % splitTime;
      // console.log(new Date(endTime))
      if (mod > 0) {
        endTime += splitTime - mod;
        // console.log(new Date(endTime))
      }
      let dateArray = [];

      while (count-- > 0) {
        if (endTime + count * splitTime < start) {
          let d = new Date();
          d.setTime(endTime + count * splitTime);
          dateArray.push(d);
        }
      }
      return dateArray;
    },
    _timeFormatting: function(date) {
      if (date) {
        return (date.getHours() > 9 ? date.getHours() : '0' + date.getHours()) + ":" + (date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes())
      } else {
        return "00:00"
      }
    },
    getTimeList: function(day, time, count) {
      let isEven = i => i % 2 === 0;
      let dateArr = this._getDateArray(day, time, count).reverse();
      // let evens = dateArr.filter(
      //   (subArr, idx) => isEven(idx)
      // )
      // let odds = dateArr.filter(
      //   (subArr, idx) => !isEven(idx)
      // )
      // console.log(dateArr)
      let dateList = ["立即自提"]
      let intervalTime = dateArr.map((v, i) => {
        if (isEven(i)) {
          // console.log(this._timeFormatting(dateArr[i]) + '-' + this._timeFormatting(dateArr[i + 1]))
          dateList.push(this._timeFormatting(dateArr[i]) + '-' + this._timeFormatting(dateArr[i + 1]))
        }
      })
      this.setData({
        intervalDate: dateList
      })
    },
    showModel: function() {
      const that = this;
      this.setData({
        modelHidden: false
      }, () => {
        let animation = wx.createAnimation({
          duration: 600,
          timingFunction: '"ease"',
        })
        this.animation = animation;
        setTimeout(function() {
          that.fadeIn();
        }, 200)
      })
    },
    hideModel: function() {
      const that = this;
      let animation = wx.createAnimation({
        duration: 800,
        timingFunction: '"ease"'
      })
      this.animation = animation;
      that.fadeDown();
      setTimeout(function() {
        that.setData({
          modelHidden: true
        })
      }, 200) //先执行下滑动画，再隐藏模块 
    },
    // 动画效果
    fadeIn: function() {
      this.animation.translateY(0).step();
      this.setData({
        animationData: this.animation.export()
      })
    },
    fadeDown: function() {
      this.animation.translateY(300).step();
      this.setData({
        animationData: this.animation.export()
      })
    },
    clickCancel: function() {
      this.hideModel();
      this.triggerEvent('pickUpTime');
    }
  },
  attached: function() {
    this.getTimeList()

    let weeks = ["日", "一", "二", "三", "四", "五", "六"]
    let today = "今天（星期" + weeks[new Date().getDay()] + "）"
    let tomorrow = "明天（星期" + weeks[new Date().getDay() + 1] + "）"
    this.setData({
      dayList: [{
          time: today,
          id: new Date().getTime()
        },
        {
          time: tomorrow,
          id: new Date().getTime() + 24 * 60 * 60 * 1000
        },
      ]
    })
  }
})